package com.gmss.crawler.module.test.serviece.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gmss.crawler.module.company.domain.Company;
import com.gmss.crawler.module.company.service.ICompanyService;
import com.gmss.crawler.module.test.domain.PersonInfoTopy;
import com.gmss.crawler.module.test.mapper.YouthMapper;
import com.gmss.crawler.module.test.serviece.IYouthService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class IYouthServiceImpl extends ServiceImpl<YouthMapper, PersonInfoTopy> implements IYouthService {

    @Resource
    private ICompanyService iCompanyService;

    @Override
    public void queryCompany(PersonInfoTopy personInfoTopy) {
        //1.personInfoTopy中是否有公司名称
        if (StringUtils.isNotBlank(personInfoTopy.getWorkCompany())){
            //如果有公司名称，和数据库的公司名称做对比
            LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Company::getCompanyName,personInfoTopy.getWorkCompany().trim());
            List<Company> list = iCompanyService.list(queryWrapper);
            if (CollectionUtils.isNotEmpty(list)){
                personInfoTopy.setCompanyId(list.get(0).getId());
                System.out.println(list.get(0).getId());
            }else {
                Company company = new Company();
                if (StringUtils.isNotBlank(personInfoTopy.getWorkCompany())) {
                    company.setCompanyName(personInfoTopy.getWorkCompany());
                }
                if (null != personInfoTopy.getCompanyType()) {
                    company.setCompanyType(personInfoTopy.getCompanyType());
                }
                if (StringUtils.isNotBlank(personInfoTopy.getCompanyAddress())) {
                    company.setAddress(personInfoTopy.getCompanyAddress());
                }
                iCompanyService.save(company);
                personInfoTopy.setCompanyId(company.getId());
            }
        }
    }
}
